#############################################################
#                        作者：我.doc                        #
# Github地址：https://github.com/idocx/WHULibSeatReservation #
#############################################################

# 用于存放一些公共函数和常量

import time

# ui的字体字号
font_name = "微软雅黑"
console_font_name = "Consolas-with-Yahei"
font_size = 11

# 抢座模式下，每次搜索的时间间隔，建议不少于3s
interval_time = 5

########################
# 用于发送请求时所用的代码
########################

# 各个场馆的代码
# "不限场馆": "null",
# "信息分馆": 1,
# "工学分馆": 2,
# "医学分馆": 3,
# "总馆": 4,
lib_code = {4: "null", 1: 1, 2: 2, 3: 3, 0: 4}

# 选择房间对应的代码
# '不限房间': 'null', '3C创客空间': 4, '西自然科学区': 6, '东自然科学区': 7, '西社会科学区': 8,
# '西图书阅览区': 9, '东社会科学区': 10, '东图书阅览区': 11, '自主学习区': 12, '3C创客电子阅读': 13,
# '3C创客双屏电脑': 14, '创新学习苹果区': 15, '创新学习云桌面': 16, '201自科图书区': 19,
# '204教学参考区': 20, '302中文科技B区': 21, '305科技期刊区': 23, '402中文文科区': 24,
# '502外文区': 26, '506医学人文区': 27, '503培训教室': 28, '2楼走廊': 29, '205电阅笔记本区': 31,
# '301东自科借阅区': 32, '305中自科借阅区': 33, '401东自科借阅区': 34, '405中期刊阅览区': 35,
# '501东外文借阅区': 37, '505中自科借阅区': 38, 'A1座位区': 39, 'A2借阅区': 51, 'A3': 52,
# 'A4': 60, 'A5': 61, 'A1沙发区': 62, 'A1电子阅览室': 63, 'B1': 65, 'A1苹果区': 66, '205电阅云桌面区': 68,
# 'A1手提电脑区': 76, 'E2 报刊阅览区': 84, 'E3 学位论文阅览区': 85, 'E4 港台文献阅览区': 86,
# 'E5 地方文献阅览区': 87, 'E6 影印文献（古籍/民国）阅览区': 88, 'E2大厅': 89, '创新学习讨论区': 90,
# 'E1信息共享空间': 91, 'E1信息共享空间双屏云桌面区': 92
room_code = {
    0: {0: "null", 1: 39, 2: 51, 3: 52, 4: 60, 5: 61, 6: 62, 7: 63, 8: 76, 9: 84, 10: 85,
        11: 86, 12: 87, 13: 88, 14: 89, 15: 91, 16: 92, 17: 94, 18: 102, 19: 103, 20: 104,
        21: 107},

    1: {0: "null", 1: 4, 2: 6, 3: 7, 4: 8, 5: 10, 6: 11, 7: 12,8: 13, 9: 14, 10: 15, 11: 16,
        12: 90},

    2: {0: "null", 1: 19, 2: 29, 3: 31, 4: 32, 5: 33, 6: 34, 7: 35, 8: 37, 9: 38, 10: 68, 
    	11: 93, 12: 100, 13: 101},

    3: {0: "null", 1: 20, 2: 21, 3: 23, 4: 24, 5: 26, 6: 27},

    4: {0: "null"}
}

# 有无电源对应的代码
# "不限电源": "null",
# "有电源": 1,
# "无电源": 0,
power_code = {0: "null", 1: 1, 2: 0}

# 是否靠窗对应的代码
# "不限靠窗": "null",
# "靠窗": 1,
# "不靠窗": 0,
window_code = {0: "null", 1: 1, 2: 0}

########################
# 用于GUI中下拉菜单的选择
########################

# 开始时间列表
start_time_list = ("08:00", "08:30", "09:00", "09:30", "10:00", "10:30",
                   "11:00", "11:30", "12:00", "12:30", "13:00", "13:30",
                   "14:00", "14:30", "15:00", "15:30", "16:00", "16:30",
                   "17:00", "17:30", "18:00", "18:30", "19:00", "19:30",
                   "20:00", "20:30", "21:00", "21:30", "22:00",)

# 结束时间列表
end_time_list = ("08:30", "09:00", "09:30", "10:00", "10:30", "11:00",
                 "11:30", "12:00", "12:30", "13:00", "13:30", "14:00",
                 "14:30", "15:00", "15:30", "16:00", "16:30", "17:00",
                 "17:30", "18:00", "18:30", "19:00", "19:30", "20:00",
                 "20:30", "21:00", "21:30", "22:00", "22:30",)

# 分馆列表
building_list = ("总馆", "信息分馆", "工学分馆", "医学分馆", "不限场馆")

# 靠窗列表
window_list = ("不限靠窗", "靠窗", "不靠窗",)


power_list = ("不限电源", "有电源", "无电源",)

room_index = {
    0: ("不限房间", "A1座位区", "A2借阅区", "A3", "A4", "A5", "A1沙发区",
        "A1电子阅览室", "A1手提电脑区", "E2", "E3", "E4", "E5", "E6", "E2大厅",
        "E1", "E1双屏云桌面区", "B2自习区", "A2双屏电脑区","A2苹果电脑区", "A2视频工作站区",
        "B1"),

    1: ("不限房间", "3C创客空间", "西自然科学区", "东自然科学区", "西社会科学区",
        "东社会科学区", "东图书阅览区", "自主学习区", "3C创客电子阅读",
        "3C创客双屏电脑", "创新学习苹果区", "创新学习云桌面", "创新学习讨论区"),

    2: ("不限房间", "201自科图书区", "2楼走廊", "205电阅笔记本区", "301东自科借阅区",
        "305中自科借阅区", "401东自科借阅区", "405中期刊阅览区", "501东外文借阅区",
        "505中自科借阅区", "205电阅云桌面区", "103空间双屏云桌面区", "一楼中部走廊",
        "103支点空间"),

    3: ("不限房间", "204教学参考区", "302中文科技B区", "305科技期刊区", "402中文文科区",
        "502外文区", "506医学人文区"),

    4: ("不限房间",)
}


class TimeSetError(Exception):
    """
    时间设置不正确
    """
    pass


class LoginError(Exception):
    """
    登陆失败
    """
    pass


class ReserveStateError(Exception):
    """
    预约状态错误
    """
    pass


def time_transfer(set_time):
    """
    将“小时：分钟”表示的时间转换为分钟表示
    :param set_time: str like {}:{}
    :return: str，minute
    """
    hours, minutes = [int(time_element) for time_element in set_time.split(":")]
    return hours * 60 + minutes


def get_reserve_date():
    """
    获取应该预约的日期
    :return: 应该预约的日期的字符串，以及是否为预约第二天的座位
    """
    time_in_sec = time.time()
    hour, minute = time.localtime(time_in_sec)[3:5]
    is_tomorrow = False

    if (hour * 60 + minute) > 1350:  # 如果时间超过22:30，则对当前日期自动加一，自动预约第二天的座位（非常智能）
        time_in_sec += 86400
        is_tomorrow = True

    exact_time = time.localtime(time_in_sec)
    date = (exact_time[i] for i in range(3))
    return "{0}-{1:02}-{2:02}".format(*date), is_tomorrow


def is_reasonable_time(start_time, end_time, is_tomorrow):
    """
    用于判断预约时间是否合法
    :param is_tomorrow: 是否为第二天
    :param start_time: 开始时间，整数（分钟）
    :param end_time: 结束时间，整数（分钟）
    :return: Bool
    """
    if end_time <= start_time:
        return False

    if start_time < 480 or end_time > 1350:
        return False

    current_time_in_sec = time.time()
    hour, minute = time.localtime(current_time_in_sec)[3:5]
    current_time = hour * 60 + minute
    if start_time <= current_time and not is_tomorrow:
        return False

    return True


def get_rest_time():
    """
    如果时间在22:30~22:45之间，则会开启等候预约模式
    :return:
    """
    *date, hour, minute, second = time.localtime(time.time())[:6]
    print("登陆时间为{3:02}:{4:02}:{5:02}\n"
          "日期为{0}年{1:02}月{2:02}日".format(*date, hour, minute, second))

    # 计算出时间差
    time_rest = 0
    if 1350 < (hour * 60 + minute) < 1365:
        time_rest = 2702 - (minute * 60 + second)
        print("进入等待模式，在将22:45自动开始预约")
    return time_rest
